<?php
namespace app\index\model;

use app\common\model\SuperModel;
use app\index\controller\User;
use app\index\model\DCharacter;
use app\index\model\Register;

class Ban extends SuperModel
{

    protected $connection = ['prefix' => 'acc_dragon_'];

    public function _initialize()
    {
        parent::_initialize();

        $this->table     = 'acc_dragon_ban';
        $this->character = new DCharacter();
        $this->user      = new User();
    }
    /**
     * 系统用户封禁
     */
    public function banUserAccount($id, $reason, $deadline)
    {

        try {

            //添加封禁日志

            $this::create([
                'uid'       => $id,
                'reason'    => $reason,
                'deadline'  => $deadline,
                'operation' => 2,
            ]);

            $output['success'] = true;
            $output['msg']     = '封号操作成功';

            //封禁日志
            //
            $this->doLog($output);

            _callback($output);

        } catch (\Exception $e) {
            throw new \think\Exception('异常消息:' . $e->getMessage());
        }
    }
    /**
     * 账户封禁
     */
    public function banAccount($ids, $reason, $deadline)
    {

        try {

            //状态修改
            foreach ($ids as $key => $id) {

                //账号封禁
                $this->banAccountByCharacter($id, 0);

                //添加封禁日志
                $newBan = $this::create([
                    'uid'       => $id,
                    'reason'    => $reason,
                    'deadline'  => $deadline,
                    'operation' => 1,
                ]);

            }

            $output['success'] = true;
            $output['msg']     = '封号操作成功';

            //封禁日志
            //
            $this->doLog($output);

            _callback($output);

        } catch (\Exception $e) {
            throw new \think\Exception('异常消息:' . $e->getMessage());
        }
    }
    /**
     * 账户解封
     */
    public function unbanAccount($ids)
    {
        try {

            foreach ($ids as $key => $id) {

                //账号解封
                $this->banAccountByCharacter($id, 1);

                //增加解封标记
                $this::where('uid', $id)
                    ->where('operation', 1)
                    ->update(['unsealed' => 1]);
            }

            $output['success'] = true;
            $output['msg']     = '解封操作成功';

            //封禁日志
            //
            $this->doLog($output);

            _callback($output);

        } catch (\Exception $e) {
            throw new \think\Exception('异常消息:' . $e->getMessage());
        }
    }

    /**
     * 角色禁言
     */
    public function banComment($ids, $reason, $deadline)
    {
        try {

            //角色ID禁言状态修改
            foreach ($ids as $key => $id) {

                DCharacter::changeDontalkTo($id, 1);

                //添加封禁日志
                $newBan = $this::create([
                    'uid'       => $id,
                    'reason'    => $reason,
                    'deadline'  => $deadline,
                    'operation' => 0,
                ]);

            }

            $output['success'] = true;
            $output['msg']     = '禁言操作成功';

            //封禁日志
            //
            $this->doLog($output);

            _callback($output);

        } catch (\Exception $e) {
            throw new \think\Exception('异常消息:' . $e->getMessage());
        }
    }
    /**
     * 禁言解封
     */
    public function unbanComment($ids)
    {

        try {

            foreach ($ids as $key => $id) {

                DCharacter::changeDontalkTo($id, 0);

                //增加解封标记
                $this::where('uid', $id)
                    ->where('operation', 0)
                    ->update(['unsealed' => 1]);

            }

            $output['success'] = true;
            $output['msg']     = '解禁操作成功';

            //封禁日志
            //
            $this->doLog($output);

            _callback($output);

        } catch (\Exception $e) {
            throw new \think\Exception('异常消息:' . $e->getMessage());
        }
    }
    /**
     * 封禁账号操作
     *
     * @param    int $id 玩家角色ID
     * @param    int $value 是否可以登录1可以 0禁止
     * @return   void
     */
    public function banAccountByCharacter($id, $value)
    {
        try {
            //账号封禁
            $user = $this->queryUserByCharacter($id);
            $user->save(['enable' => $value]);

        } catch (\Exception $e) {
            throw new \think\Exception('异常消息:' . $e->getMessage());
        }
    }
    /**
     * 通过账户角色ID查询用户账号
     *
     * @param    int $id 角色ID
     * @return   object  用户对象
     */
    public function queryUserByCharacter($id)
    {
        $character = DCharacter::getById($id);

        $uid = $character->userCharacter->getData('uid');
        return Register::getById($uid);
    }
}
